
export const getCellAttr = (cellObj, cellColAttrs, rowHeight) => {
    let cellAttr = {} as any;
    let styleObj = {} as any;
    let innerStyleObj = {} as any;
    let className = "";

    //单元格边框
    appendBorder(cellObj, styleObj);
    //单元格样式
    appendStyle(cellObj, styleObj, innerStyleObj, true);
    const financial = cellObj && cellObj["financial"];
    //财务格式所在单元格div增加高度100%
    if (financial) {
        styleObj.height = rowHeight;
        styleObj.lineHeight = `${rowHeight}px`;
        innerStyleObj.height = "100%";
    }

    //列自定义属性
    if (cellColAttrs && cellColAttrs.get("hide") === "y")
        styleObj.display = "none";
    if (cellColAttrs && cellColAttrs.get("class"))
        className += " " + cellColAttrs.get("class");
    //单元格自定义属性
    const cusattrs = cellObj["attrs"];
    cusattrs && appendCusAttrObj(cellAttr, cusattrs, innerStyleObj);

    cellAttr.styleObj = styleObj;
    cellAttr.innerStyleObj = innerStyleObj;
    cellAttr.class = className;
    return cellAttr;
}

export const getMcCellAttr = (cellObj) => {
    let mcCellAttr = {} as any;
    let styleObj = {};
    let innerStyleObj = {};

    //单元格样式
    appendStyle(cellObj, styleObj, innerStyleObj, true);
    for (var key in styleObj) {
        if (key !== "text-align" && key != "vertical-align")
            innerStyleObj[key] = styleObj[key];
    }
    //自定义属性
    const cusattrs = cellObj.attrs;
    cusattrs && appendCusAttrObj(mcCellAttr, cusattrs, innerStyleObj);

    mcCellAttr.innerStyleObj = innerStyleObj;
    return mcCellAttr;
}

export const getRowAttr = (rowHeight, rowCusAttr) => {
    let rowAttr = {} as any;
    rowAttr.id = rowCusAttr ? rowCusAttr.get("id") : "";
    rowAttr.name = rowCusAttr ? rowCusAttr.get("name") : "";
    rowAttr.class = rowCusAttr ? rowCusAttr.get("class") : "";
    let styleObj = {};
    styleObj["height"] = rowHeight;
    if (rowCusAttr && rowCusAttr.get("hide") === "y")
        styleObj["display"] = "none";
    if (rowCusAttr && rowCusAttr.get("style"))
        appendCusAttrStyle(styleObj, rowCusAttr.get("style"));
    rowAttr.styleObj = styleObj;
    return rowAttr;
}


const appendBorder = (cellObj, styleObj) => {
    const eBorder = cellObj["eborder"];
    const etype = cellObj && cellObj["etype"];
    eBorder && eBorder.map((v) => {
        const { kind, color, style } = v;
        if (etype === "7" && (kind === "top" || kind === "bottom"))  //明细所在区域不解析上下边框
            return true;
        if (color)
            styleObj["border-" + kind + "-color"] = color;
        const borderstyle = style ? parseInt(style) : 0;
        if (borderstyle === 0) {
        } else if (borderstyle === 2) {
            styleObj["border-" + kind + "-width"] = "2px";
            styleObj["border-" + kind + "-style"] = "solid";
        } else if (borderstyle === 3) {
            styleObj["border-" + kind + "-width"] = "1px";
            styleObj["border-" + kind + "-style"] = "dashed";
        } else if (borderstyle === 5) {
            styleObj["border-" + kind + "-width"] = "3px";
            styleObj["border-" + kind + "-style"] = "solid";
        } else if (borderstyle === 6) {
            styleObj["border-" + kind + "-width"] = "3px";
            styleObj["border-" + kind + "-style"] = "double";
        } else if (borderstyle === 7) {
            styleObj["border-" + kind + "-width"] = "1px";
            styleObj["border-" + kind + "-style"] = "dotted";
        } else if (borderstyle === 8) {
            styleObj["border-" + kind + "-width"] = "2px";
            styleObj["border-" + kind + "-style"] = "dashed";
        } else {
            styleObj["border-" + kind + "-width"] = "1px";
            styleObj["border-" + kind + "-style"] = "solid";
        }
        if (!styleObj["vertical-align"]) {
            styleObj["vertical-align"] = "middle";
        }
    })
    return styleObj;
}

const appendStyle = (cellObj, styleObj, innerStyleObj, isMc) => {
    const etype = cellObj && cellObj["etype"];
    if (etype === "7" || etype === "12")
        return;
    //背景色
    if (cellObj["backgroundColor"])
        styleObj["backgroundColor"] = cellObj["backgroundColor"];
    //单元格图片
    if (etype === "6") {
        styleObj["backgroundImage"] = "url('" + cellObj["field"] + "') !important";
        styleObj["backgroundRepeat"] = "no-repeat !important";
    }
    const font = cellObj["font"];
    if (font) {
        if (font["text-align"]) styleObj["text-align"] = font["text-align"];
        if (font["vertical-align"]) styleObj["verticalAlign"] = font["vertical-align"];
        if (font["bold"] == "true") innerStyleObj["font-weight"] = "bold";
        if (font["italic"] == "true") innerStyleObj["font-style"] = "italic";
        if (font["underline"] == "true") innerStyleObj["text-decoration"] = "underline";
        if (font["deleteline"] == "true") innerStyleObj["text-decoration"] = "line-through";
        if (font["font-size"]) {
            styleObj["font-size"] = font["font-size"];
            innerStyleObj["font-size"] = font["font-size"];
            let fontsize = parseInt(font["font-size"], 0);
            if (etype == "3" && fontsize > 9) {
                let suitheight = fontsize_compare_height(fontsize);
                styleObj["height"] = suitheight + "px";
                styleObj["ine-height"] = suitheight + "px";
            }
        }
        if (font["color"]) styleObj["color"] = font["color"];
        if (font["font-family"]) styleObj["font-family"] = font["font-family"];
        if (font["font-family"]) innerStyleObj["font-family"] = font["font-family"];
    }
    //默认样式
    if (!styleObj["text-align"]) styleObj["text-align"] = "left";
    if (!styleObj["verticalAlign"]) styleObj["verticalAlign"] = (isMc ? "middle" : "top");
    if (!innerStyleObj["font-size"]) innerStyleObj["font-size"] = "12px";
    if (!innerStyleObj["font-family"]) innerStyleObj["font-family"] = "Microsoft YaHei";
    //缩进
    let etxtindent = cellObj["etxtindent"];
    if (etxtindent && etxtindent !== "0") {
        etxtindent = parseFloat(etxtindent) * 8 + "px";
        if (styleObj["text-align"] === "left")
            styleObj["padding-left"] = etxtindent;
        else if (styleObj["text-align"] === "right")
            styleObj["padding-right"] = etxtindent;
    }
    innerStyleObj["word-break"] = "break-all";
    innerStyleObj["word-wrap"] = "break-word";
}

const appendCusAttrObj = (obj, cusattrs, styleObj: any) => {
    if (!cusattrs)
        return;
    for (let k in cusattrs) {
        let v = cusattrs[k];
        if (k === "hide") {
            if (v === "y")
                styleObj.display = "none";
        } else if (k === "id") {
            obj.cusid = v;
        } else if (k === "name") {
            obj.cusname = v;
        } else if (k === "class") {
            obj.cusclass = v;
        } else if (k === "style") {
            appendCusAttrStyle(styleObj, v);
        } else {  //自定义属性加data-并且变小写
            obj[`data-${k.toLowerCase()}`] = v;
        }
    }
}

//根据解析的自定义属性生成单元格属性
export const buildCellAttr = (tdCellAttr, className, style) => {
    let cellAttr = {} as any;
    if (className)
        cellAttr.className = className;
    if (style) {
        cellAttr.style = style;
    }
    if (!tdCellAttr)
        return cellAttr;
    for (let key in tdCellAttr) {
        const value = tdCellAttr[key];
        if (key === "cusid")
            cellAttr.id = value;
        else if (key === "cusname")
            cellAttr.name = value;
        else if (key === "styleObj")
            cellAttr.styleObj = value;
        else if (key.length > 5 && key.substring(0, 5) === "data-")     //自定义属性
            cellAttr[key] = value;
    }
    return cellAttr
}

const appendCusAttrStyle = (styleObj, cusstylestr) => {
    let styleArr = cusstylestr.split(";");
    for (let i = 0; i < styleArr.length; i++) {
        let item = styleArr[i];
        let itemArr = item.split(":");
        if (itemArr.length >= 2)
            styleObj[itemArr[0]] = itemArr[1];
    }
    return styleObj;
}

const fontsize_compare_height = (fontsize) => {
    if (fontsize <= 10) {
        return 24;
    } else if (fontsize <= 12) {
        return 26;
    } else if (fontsize <= 24) {
        return fontsize * 2;
    } else if (fontsize <= 26) {
        return 50;
    } else if (fontsize <= 28) {
        return 52;
    } else if (fontsize <= 32) {
        return 58;
    } else if (fontsize <= 48) {
        return 82;
    } else {
        return 108;
    }
}